﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons.Web.Helpers;
using Commons.Data;
using Web.Attributes;

namespace TestHttpModules.Controllers
{
    public class RegTokenHelper
    {
        static List<string> _key;
        static RegTokenHelper()
        {
            _key = new List<string>();
        }
        public static void GenerateToken(string key)
        {
            _key.Add(key);
        }
        public static void DeleteToken(string key)
        {
            _key.Remove(key);
        }
        public static bool FindTokenByKey(string key)
        {
            return _key.Contains(key);
        }
    }
    public class ProductController : Controller
    {
        #region Constructors
        IRepository iRepository;
        public ProductController()
        {
            iRepository = new Repository();
        }
        #endregion

        #region 产品相关
        public ActionResult Index()
        {
            return View();
        }
        [UserAuthentication(AuthenticationType.Login)]
        [HttpPost]
        public ActionResult Index(Product_Comment entity)
        {
            if (ModelState.IsValid)
            {
                new Repository().Insert<Product_Comment>(entity);
                return RedirectToAction("Index");
            }
            return View();
        }

        public PartialViewResult List()
        {
            var linq = from data1 in new Repository().GetEntities<Product>()
                       join data2 in new Repository().GetEntities<Product_Comment>() on data1.ID equals data2.ProductID into list
                       select new Product_Ext
                       {
                           ID = data1.ID,
                           Info = data1.Info,
                           Name = data1.Name,
                           Price = data1.Price,
                           Product_Comment = list,
                           Status = data1.Status,
                           UserID = data1.UserID,
                       };
            return PartialView(linq.ToList());
        }

        public ActionResult Delete(int id)
        {

            iRepository.Delete<Product>(new Product { ID = id });
            return RedirectToAction("Index");
        }
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Product entity)
        {
            new Repository().Insert<Product>(entity);
            return RedirectToAction("Index");
        }
        #endregion

        #region 产品评论相关
        /// <summary>
        /// 添加评论的页面
        /// GET请求，ＰＯＳＴ操作在父页面完成，因为有用户登陆跳转功能
        /// </summary>
        /// <param name="productid"></param>
        /// <returns></returns>
        //public PartialViewResult AddProductComment(int id)
        //{
        //    ViewBag.ProductID = id;
        //    return PartialView();
        //}
        
      
        /// <summary>
        /// 商品评论列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductComment()
        {
            return View(new Repository().GetEntities<Product_Comment>().ToList());
        }
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ProductCommentDelete(int id)
        {
            iRepository.Delete<Product_Comment>(new Product_Comment
            {
                ID = id
            });
            return RedirectToAction("Index");
        }
        #endregion

    }
}
